package com.bianguo.android.functiondemo.view.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.bianguo.android.functiondemo.event.EmptyEvent;
import com.bianguo.android.functiondemo.model.BaseModel;
import com.bianguo.android.functiondemo.presenter.BasePresenter;
import com.bianguo.android.functiondemo.utils.MLog;
import com.bianguo.android.functiondemo.view.IBaseView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity<V extends IBaseView, M extends BaseModel, P extends BasePresenter<V, M>> extends AppCompatActivity {

    private Unbinder unbinder;
    private EventBus eventBus;
    protected Activity mContext;
    protected P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(layoutId());
        unbinder = ButterKnife.bind(this);
        eventBus = EventBus.getDefault();
        eventBus.register(this);
        initView(savedInstanceState);
    }

    protected void immersive() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色透明
            window.setStatusBarColor(Color.TRANSPARENT);

            View decorView = window.getDecorView();
            int visibility = decorView.getSystemUiVisibility();
            //布局内容全屏展示
            visibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            //隐藏虚拟导航栏（虚拟按钮）
            visibility |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            //防止内容区域大小发生变化
            visibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(visibility);
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EmptyEvent event) {
        MLog.mLog("BaseActivity EmptyEvent");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        boolean registered = eventBus.isRegistered(this);
        if (registered) {
            eventBus.unregister(this);
        }
        unbinder.unbind();
    }

    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    public void showView(View view) {
        if (View.VISIBLE != view.getVisibility()) {
            view.setVisibility(View.VISIBLE);
        }
    }

    public void hideView(View view, int visibility) {
        if (View.VISIBLE == view.getVisibility()) {
            view.setVisibility(visibility);
        }
    }

    public void hideView(View view) {
        hideView(view, View.GONE);
    }

    public abstract int layoutId();

    public abstract void initView(@Nullable Bundle savedInstanceState);
}
